import { defineCommand, safeGetComponent } from './utils'

// 表格定位命令
defineCommand({
  meta: {
    id: 'table:locate-row',
    name: '定位表格行',
    description: '定位并高亮显示表格中的特定行',
    category: 'data',
    argsSchema: {
      tableId: 'string',
      rowId: 'string',
    },
  },
  execute: async (context, { tableId, rowId }) => {
    try {
      // 安全获取表格组件实例
      const tableComponent = safeGetComponent<{
        locateRow: (id: string) => Promise<void>
      }>(context, tableId)

      // 调用组件方法
      await tableComponent.locateRow(rowId)

      return {
        success: true,
        message: `行 "${rowId}" 定位成功`,
      }
    } catch (error: any) {
      return {
        success: false,
        message: `定位失败: ${error.message}`,
      }
    }
  },
})

// 表格刷新命令
defineCommand({
  meta: {
    id: 'table:refresh',
    name: '刷新表格',
    description: '重新加载表格数据',
    category: 'data',
    argsSchema: {
      tableId: 'string',
      force: 'boolean',
    },
  },
  execute: async (context, { tableId, force = false }) => {
    try {
      // 安全获取表格组件实例
      const tableComponent = safeGetComponent<{
        refreshData: (force?: boolean) => Promise<void>
      }>(context, tableId)

      // 调用组件方法
      await tableComponent.refreshData(force)

      return {
        success: true,
        message: `表格刷新成功`,
      }
    } catch (error: any) {
      return {
        success: false,
        message: `刷新失败: ${error.message}`,
      }
    }
  },
})
